package com.jml.zlhq.app.ui;

import java.util.Map;

import org.json.JSONObject;

import com.baidu.mobads.AdView;
import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.CommonDialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class TixianActivity extends BaseActivity{
    private LinearLayout das;
    public static boolean freshPage;
    private LinearLayout rightLay,zfbadd,zfblay;
    private TextView jinbi,jine,zfbinfo,zfbaddtext;//,yhktx;
    private JSONObject data,zfbif;
    private float my;
    public static final float txxz = 500.0f;
    public static final String txxzstr = "500金币";
    private boolean havezfb = false;
    private ScrollView scroll;
    
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 1:
                jinbi.setText(Html.fromHtml("当前金币：<font color='#e95847'>"+BaseTools.getFrom("account_fees", data, "0")+"</font>"));
                jine.setText(Html.fromHtml("可提现：<font color='#e95847'>￥"+((float)Integer.parseInt(BaseTools.getFrom("account_fees", data, "0"))/(float)100)+"</font>"));
                havezfb = !StringUtil.isEmpty(BaseTools.getFrom("auth_info.alipay_name", data));
                //zfbinfo.setText(havezfb?("已绑定："+BaseTools.getFrom("account", data, "")):"");
                //zfbaddtext.setText(havezfb?"修改支付宝":"绑定支付宝");
                zfbadd.setVisibility(havezfb?View.GONE:View.VISIBLE);
                scroll.smoothScrollTo(0, 0);
                break;
            case 2:
                if(zfbif==null){
                    final CommonDialog show = new CommonDialog(activity, true);
                    show.show(true, true, false, "提示", "抱歉，系统出错，请稍后再试！", "确定", "", new OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            show.dismiss();
                        }
                    }, null);
                } else if(StringUtil.isEmpty(BaseTools.getFrom("account",zfbif))||BaseTools.getFrom("account",zfbif).equals("null")){
                    final CommonDialog show = new CommonDialog(activity, true);
                    show.show(true, true, false, "提示", "未绑定支付宝，请绑定！", "确定", "", new OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            show.dismiss();
                        }
                    }, null);
                } else{
                    Bundle adata = new Bundle();
                    adata.putString("jine", BaseTools.getFrom("money", data, "0"));
                    adata.putString("zfb", BaseTools.getFrom("account",zfbif));
                    adata.putFloat("jinef", my);
                    UIUtil.openActivity(TixianActivity.this,MoneyActivity.class,adata);
                }
                break;
            default:
                break;
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tixian_activity);
        LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
        rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
        leftLay.setOnClickListener(this);
        rightLay.setOnClickListener(this);
        AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
        centerEdit.setVisibility(View.GONE);
        ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
        leftImage.setImageResource(R.drawable.back_selector);
        ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
        roghtImage1.setVisibility(View.GONE);
        TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
        centerText.setText("提现");
        TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
        rightText.setVisibility(View.VISIBLE);
        rightText.setText("提现记录");
        TextView centerChosed = (TextView) findViewById(R.id.content_header_center_text2);
        centerChosed.setVisibility(View.GONE);
        
        zfbadd = (LinearLayout) findViewById(R.id.tixianat_zfbadd);
        jinbi = (TextView) findViewById(R.id.tixianat_jinbi);
        jine = (TextView) findViewById(R.id.tixianat_jine);
        zfblay = (LinearLayout) findViewById(R.id.tixianat_zfb_lay);
        zfbinfo = (TextView) findViewById(R.id.tixianat_zfb_info);
        zfbaddtext = (TextView) findViewById(R.id.tixianat_zfbadd_text);
        zfbaddtext.setText("绑定支付宝");
        zfbinfo.setText("");
        scroll = (ScrollView)findViewById(R.id.tixianat_scroll);
        //yhktx = (TextView) findViewById(R.id.tixianat_yhk);
        jinbi.setText(Html.fromHtml("当前金币：<font color='#e95847'>0</font>"));
        jine.setText(Html.fromHtml("可提现：￥<font color='#e95847'>0</font>"));
        zfbadd.setOnClickListener(this);
        zfblay.setOnClickListener(this);
        //yhktx.setOnClickListener(this);
        
        das = (LinearLayout) findViewById(R.id.tixianat_ads);
        // 实例化广告条
        AdView adView = new AdView(this,"2422493"); 
        // 将广告条加入到布局中
        das.addView(adView);        
        
        freshPage = false;
        loadData(null);
                
       // return pageView;
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==303){
            if(appContext.isLogin()){
                onClick(rightLay);
            }else{
                UIUtil.ToastMessage(activity, "未登录!");
            }
        }
        if(requestCode==406){
            if(appContext.isLogin()){
                havezfb = true;
            }else{
                UIUtil.ToastMessage(activity, "未登录!");
            }
        }
        if(requestCode==408){
            if(appContext.isLogin()){
                onClick(zfblay);
            }else{
                UIUtil.ToastMessage(activity, "未登录!");
            }
        }
        if(requestCode==409){
            if(resultCode==Activity.RESULT_OK){
                
            }else{
                UIUtil.ToastMessage(activity, "支付宝未绑定!");
            }
        }
        if(requestCode==204){
            if(StringUtil.isEmpty(BaseTools.getFrom("phone", appContext.getLoginUser()))||!StringUtil.isPhone(BaseTools.getFrom("phone", appContext.getLoginUser()))){
                UIUtil.ToastMessage(activity, "未绑定手机号!");
            }else{
                onClick(zfblay);
            }
        }
    }
    
    @Override
    public void onResume() {
        pageFresh = freshPage;
        super.onResume();
        freshPage = false;
    }
    

    @Override
    public void loadData(Map<String, String> param) {
        havezfb = false;
        zfbaddtext.setText("绑定支付宝");
        data = null;
        zfbaddtext.setVisibility(View.VISIBLE);
        if(!appContext.isLogin()){
            UIUtil.ToastMessage(activity, "未登陆");
            zfbaddtext.setVisibility(View.GONE);
        }else{
            data = appContext.getLoginUser();
        }
        mHandler.sendEmptyMessage(1);
//      if(appContext.isLogin()){
//          if(param==null)param = new HashMap<String, String>();
//          param.put("access-token", appContext.getLoginUid());
//          HttpService.postByText(URLs.MAIN_TANDIAN, param, new TextHttpResponseHandler() {
//              @Override
//              public void onSuccess(int arg0, Header[] arg1, String resultStr) {
//                  try {
//                      JSONObject result = new JSONObject(resultStr);
//                      if(result.getInt("status")==0){
//                          data = result.getJSONObject("data");
//                          JSONArray ls = result.get("data").toString().equals("null")?new JSONArray():data.getJSONArray("latestcash");
//                          for (int i = 0; ls!=null && i < ls.length(); i++) {
//                              listItems.add(ls.getJSONObject(i));
//                          }
//                      }else{
//                          UIUtil.ToastMessage(activity, "抱歉，访问失败");
//                      }
//                      mHandler.sendEmptyMessage(1);
//                  } catch (JSONException e) {
//                      e.printStackTrace();
//                  }
//              }
//              
//              @Override
//              public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
//                  UIUtil.ToastMessage(activity, "抱歉，访问失败");
//              }
//          });
//      }else{
//          data = null;
//          listItems.clear();
//          UIUtil.ToastMessage(activity, "未登陆");
//          mHandler.sendEmptyMessage(1);
//      }
    }

    @Override
    public void clearData() {
        pageEnd = false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.content_header_left_lay:
            finish();
            break;
        case R.id.content_header_right_lay:
            if(appContext.isLogin()){
                Bundle data1 = new Bundle();
                data1.putString("title", "提现记录");
                data1.putInt("type", 3);
                data1.putString("url", URLs.MAIN_TIXIAN_HISTORY);
                UIUtil.openActivity(this, JinbiHistoryActivity.class, data1);
            }else{
                UIUtil.openActivity(this, LoginActivity.class, null,303);
            }
            break;
        case R.id.tixianat_zfbadd:
            if(appContext.isLogin()){
                UIUtil.openActivity(activity, BindZfbActivity.class, null);
            }else{
                UIUtil.openActivity(this, LoginActivity.class, null,406);
            }
            
            break;
//      case R.id.tixianat_yhk:
//          final CommonDialog show = new CommonDialog(activity, true);
//          show.show(true, true, false, "提示", "暂时未开发，敬请期待！", "确定", "", new OnClickListener(){
//              @Override
//              public void onClick(View v) {
//                  show.dismiss();
//              }
//          }, null);
//          
//          break;
        case R.id.tixianat_zfb_lay:
            if(appContext.isLogin()){
                if(!havezfb){
                    UIUtil.openActivity(activity, BindZfbActivity.class, null,409);
                }else if(StringUtil.isEmpty(BaseTools.getFrom("phone", appContext.getLoginUser()))||!StringUtil.isPhone(BaseTools.getFrom("phone", appContext.getLoginUser()))){
                    UIUtil.openActivity(activity, BindPhoneActivity.class, null,204);
                }else{
                my = 0f;
                try {
                    my = Float.parseFloat(BaseTools.getFrom("account_fees", data, "0"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(my<txxz){
                    final CommonDialog show2 = new CommonDialog(activity, true);
                    show2.show(true, true, false, "提示", "金币不足，最低提现金币为"+TanDianFragment.txxzstr+"！", "确定", "", new OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            show2.dismiss();
                        }
                    }, null);
                }else{
                    if(!havezfb){
                        final CommonDialog show = new CommonDialog(activity, true);
                        show.show(true, true, false, "提示", "未绑定支付宝，请绑定！", "确定", "", new OnClickListener(){
                            @Override
                            public void onClick(View v) {
                                show.dismiss();
                            }
                        }, null);
                    } else{
                        Bundle adata = new Bundle();
                        adata.putString("jine", BaseTools.getFrom("account_fees", data, "0"));
                        adata.putString("zfb", BaseTools.getFrom("auth_info.alipay_name", data, ""));
                        adata.putString("zfbxm", BaseTools.getFrom("auth_info.real_name", data, ""));
                        adata.putFloat("jinef", my);
                        UIUtil.openActivity(TixianActivity.this,MoneyActivity.class,adata);
                    }
                }
                }
            }else{
                UIUtil.openActivity(this, LoginActivity.class, null,408);
            }
            
            break;
        default:
            break;
        }
    }

}
